/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.base {
	import com.googlecode.milib.data.value.MLNumberValue;
	import com.googlecode.milib.service.singleton.EnterFrameService;
	import com.googlecode.milib.vo.MLEvent;

	/**
	 * @author Marek Brun
	 */
	public class MLWorkClass extends MLActionClass {

		private var startTime : Number;
		protected var workingTime : Number;
		protected var workingTimeDN : MLNumberValue;
		private var startWorkingTime :Number;
		public var isForceFinish:Boolean=true;

		public function MLWorkClass() {
			
		}//<>
		/*abstract*/ protected function doPrepare():void {}
		/*abstract*/ protected function doEnd():void {}
		/*abstract*/ protected function doWork():void {}
		
		public function setupWorkingTime(workingTime:Number):void {
			this.workingTime=workingTime;
			workingTimeDN=null;
		}
		
		public function setupWorkingTimeAsDynamicNumber(dn:MLNumberValue):void {
			if(dn.v==0){ dn.v=1000; }
			workingTimeDN=dn;
		}
		
		public function getWorkingTime():Number {
			if(workingTimeDN){
				return workingTimeDN.v;
			}else{
				return workingTime;
			}
		}
		
		override protected function doStart():Boolean {
			_isSuccess=false;
			startTime=EnterFrameService.getInstance().getTime();
			startWorkingTime=getWorkingTime();
			if(isNaN(getWorkingTime())){
				throw new Error('Time is not setup (isNaN(getWorkingTime()))');
			}
			doPrepare();
			EnterFrameService.getInstance().addEventListener(EnterFrameService.getInstance().event_EnterFrame, onWorkEnterFrame);
			return true;
		}

		override protected function doFinish():Boolean {
			if(isForceFinish || _isSuccess){
				EnterFrameService.getInstance().removeEventListener(EnterFrameService.getInstance().event_EnterFrame, onWorkEnterFrame);
				doEnd();
				return true;
			}
			return false;
		}
		
		public function getProgress():Number {
			var progress:Number=(EnterFrameService.getInstance().getTime()-startTime)/startWorkingTime;
			if(progress>1){ progress=1; }
			return progress;
		}
		
//********************************************************************************************
//		events for MLWorkClass
//********************************************************************************************
		private function onWorkEnterFrame(event:MLEvent):void {
			doWork();
			if(EnterFrameService.getInstance().getTime()-startTime>startWorkingTime){
				_isSuccess=true;
				finish();
			}
		}
		
	}
}
